package com.example.game;

import android.app.Fragment;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.lang.reflect.Field;
import java.util.Timer;
import java.util.TimerTask;

import static com.example.game.GamePage.player;

public class setGameFragment extends Fragment
{

    private ImageButton mLeftMenu;
    final int COMPLETED = 0;
    final int STOP = 1;
    Timer timer = new Timer();
//    int news_count = 0;

    Handler handler_refresh = new Handler();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        Log.d("TAG", "onCreateView");
        final View view = inflater.inflate(R.layout.game_fragment_layout, container,false);
        final Button button_click = view.findViewById(R.id.button_click);
        int total_count = 0;
        for(int i = 0; i < 10 ; i ++ ){
            total_count += player.building_list[0][i];
        }
        final long click_bonus =  1 + player.building_list[0][10] + player.building_list[0][11] * total_count *(long) player.production / 30;
            //观看完视频后的奖励：每个建筑1会提供1%的production的“内容质量”。视频是否观看完成被保存在building_list[0][11]
        TextView textView_button = (TextView)view.findViewById(R.id.TextView_Button);
        textView_button.setText(String.format("发布一篇新的营销号文章\n产生%s营销号！", player.convertLongToString(click_bonus)));
        button_click.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Log.d("click_bonus_TAG", "onClick: " + click_bonus);
                player.cash = player.cash + click_bonus;
                Log.d("gfTAG", "onClick: button");
            }
        });
        final TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = COMPLETED;
                handler_refresh.sendMessage(msg);
            }
        };
        handler_refresh = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                /*
                    设置新闻：
                    检索当前最高级的建筑，用count_building_type记录
                    news_count记录当前播放到哪一条新闻
                    然后从player.news[][]中选取对应的news
                 */
                if (msg.what == COMPLETED) {
                    player.check_next_news();
                    if(player.news_count >= player.news[(int)player.building_list[0][13]].length){
                        player.news_count = 0;
                    }
                    TextView textView = (TextView)view.findViewById(R.id.TextView_News);
                    textView.setText(player.news[(int)player.building_list[0][13]][player.news_count]);
                    Log.d("newsTAG", "handleMessage: "+player.news_count);
                    player.news_count++;
                }
                if(msg.what == STOP){
                    timerTask.cancel();
                }
            }
        };





        timer.schedule(timerTask,0,5000);
        setTimerTask_state(timerTask);
        return view;
    }

    @Override
    public void onStop() {
        Message msg = new Message();
        msg.what = STOP;
        handler_refresh.sendMessage(msg);
        super.onStop();
    }


    public void setTimerTask_state(TimerTask timerTask) {
        Field field;            //通过反射，设置TimerTask的state属性为0，以避免Task already scheduled or cancelled报错
        try {
            field = TimerTask.class.getDeclaredField("state");
            field.setAccessible(true);
            field.set(timerTask, 0);
            Log.d("setTimerTask_state_TAG", "set state finish");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}